<?php

namespace App\Services;
use App\Models\CdnDomain;
use Illuminate\Support\Facades\Cache;

class CdnService
{
    /**
     * 图片CDN
     *
     * @return void
     */
    public function getImgCdn()
    {
        $data = [
            'state' =>'nocdn',
            'img_cdn1' => '',
            'img_cdn2' => ''
        ];
        //可用cdn数量
        $img_cdn_count = Cache::rememberForever('img_cdn_count',  function () {
            return CdnDomain::show()->img()->count();
        }); 
        if($img_cdn_count){
            //img_cdn1
            $data['img_cdn1'] = Cache::rememberForever('img_cdn1',  function () {
                //cdn有可能没有了，所以加锁防止没有时缓存穿透
                return Cache::lock('img_cdn1')->get(function () {
                    // 获取无限期锁并自动释放...
                    $domain = CdnDomain::show()->img()->select('id','domain')->first();
                    if($domain){
                        return $domain->toArray();
                    }
                });
            });
            $data['img_cdn2'] = $data['img_cdn1'];
            if(!$data['img_cdn1']){
                Cache::forget('img_cdn1');
            }
            //可用数量不止一个，设置图片备用cdn
            if($img_cdn_count>1){
                //img_cdn2
                $data['img_cdn2'] = Cache::rememberForever('img_cdn2',  function () {
                    //cdn有可能没有了，所以加锁防止没有时缓存穿透
                    return Cache::lock('img_cdn2')->get(function () {
                        // 获取无限期锁并自动释放...
                        $domain = CdnDomain::show()->img()->select('id','domain')->offset(1)->first();
                        if($domain){
                            return $domain->toArray();
                        }
                    });
                }); 
            }
            if(!$data['img_cdn2']){
                Cache::forget('img_cdn2');
            }
            if($data['img_cdn1'] || $data['img_cdn2']){
                $data['state']='success';
            }
        }else{
            //清空缓存
            Cache::forget('img_cdn1');
            Cache::forget('img_cdn2');
            Cache::forget('img_cdn_count');
        }
        return $data;
    }

    /**
     * 视频cdn
     *
     * @return void
     */
    public function getVideoCdn(){
        // $data = [
        //     'state' =>'nocdn',
        //     'video_cdn' => '',
        // ];
        // $data['video_cdn'] = Cache::rememberForever('video_cdn',  function () {
        //     //cdn有可能没有了，所以加锁防止没有时缓存穿透
        //     return Cache::lock('video_cdn')->get(function () {
        //         // 获取无限期锁并自动释放...
        //         $domain = CdnDomain::show()->video()->select('id','domain')->first();
        //         if($domain){
        //             return $domain->toArray();
        //         }
        //     });
        // });
        // if(!$data['video_cdn']){
        //     Cache::forget('video_cdn');
        // }else{
        //     $data['state']='success';
        // }
        // return $data;
        $data = [
            'state' =>'nocdn',
            'video_cdn1' => '',
            'video_cdn2' => ''
        ];
        //可用cdn数量
        $video_cdn_count = Cache::rememberForever('video_cdn_count',  function () {
            return CdnDomain::show()->video()->count();
        }); 
        if($video_cdn_count){
            //img_cdn1
            $data['video_cdn1'] = Cache::rememberForever('video_cdn1',  function () {
                //cdn有可能没有了，所以加锁防止没有时缓存穿透
                return Cache::lock('video_cdn1')->get(function () {
                    // 获取无限期锁并自动释放...
                    $domain = CdnDomain::show()->video()->select('id','domain')->first();
                    if($domain){
                        return $domain->toArray();
                    }
                });
            });
            $data['video_cdn2'] = $data['video_cdn1'];
            if(!$data['video_cdn1']){
                Cache::forget('video_cdn1');
            }
            //可用数量不止一个，设置图片备用cdn
            if($video_cdn_count>1){
                //img_cdn2
                $data['video_cdn2'] = Cache::rememberForever('video_cdn2',  function () {
                    //cdn有可能没有了，所以加锁防止没有时缓存穿透
                    return Cache::lock('video_cdn2')->get(function () {
                        // 获取无限期锁并自动释放...
                        $domain = CdnDomain::show()->video()->select('id','domain')->offset(1)->first();
                        if($domain){
                            return $domain->toArray();
                        }
                    });
                }); 
            }
            if(!$data['video_cdn2']){
                Cache::forget('video_cdn2');
            }
            if($data['video_cdn1'] || $data['video_cdn2']){
                $data['state']='success';
            }
        }else{
            //清空缓存
            Cache::forget('video_cdn1');
            Cache::forget('video_cdn2');
        }
        return $data;
    }

}
